package ui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SpinnerListModel;
import javax.swing.SwingConstants;

import bean.KeretaApiBean;
import bean.RuteBean;

import cas.CAS;
import cas.GPSSimulator;
import cas.Identity;
import dao.DAOFactory;
import dao.oracle.KeretaApiDao;

public class SouthPanel extends JPanel {

	private JLabel lblSuggest;
	private JButton btnStart;
	private MainFrame mainFrame;
	public JLabel lblTimemsg;
	/**
	 * Create the panel.
	 */
	public SouthPanel(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
		setBorder(null);
		setLayout(new BorderLayout(0, 0));

		lblSuggest = new JLabel("");
		lblSuggest.setHorizontalAlignment(SwingConstants.CENTER);
		lblSuggest.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
		add(lblSuggest, BorderLayout.CENTER);

		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.setRollover(true);
		add(toolBar, BorderLayout.EAST);

		btnStart = new JButton("Start");
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnStartActionPerformed(arg0);
			}
		});
		btnStart.setFont(new Font("Tahoma", Font.PLAIN, 15));
		toolBar.add(btnStart);
		btnStart.setIcon(getImageIcon("/icon/button_ok.png"));
		
		lblTimemsg = new JLabel("");
		add(lblTimemsg, BorderLayout.SOUTH);
	}
	private void btnStartActionPerformed(ActionEvent evt) {

		EastPanel eastPanel = mainFrame.getEastPanel();
		if(btnStart.getText().equals("Start")){
			if(!checkParameter()){
				return;
			}
			//TODO
			try {
				GPSSimulator gpsSimulator;
				mainFrame.setCas(new CAS());
				CAS cas = mainFrame.getCas();
				Identity iden = new Identity();
				KeretaApiDao kaDao = DAOFactory.createKeretaApiDao();
				KeretaApiBean kaBean = kaDao.getAvailable(eastPanel.getKaCombo().getSelectedItem().toString());
				kaBean.setAvailable(false);
				kaDao.update(kaBean);
				iden.setKeretaApi(kaBean);
				iden.setGerbong(Integer.valueOf(eastPanel.getGerbongIsi().getValue().toString()), Integer.valueOf(eastPanel.getGerbongKosong().getValue().toString()));
				List<RuteBean> rute = null;
				rute = DAOFactory.createRuteDao().getRuteWhereNamaRute(eastPanel.getRuteCombo().getSelectedItem().toString());
				iden.setRute(rute);
				gpsSimulator = new GPSSimulator(iden, 0.0, Double.valueOf(eastPanel.getKecepatanSpinner().getValue().toString()),mainFrame.getWwd().getModel().getGlobe());
				cas.setParameter(eastPanel.getPortCombo().getSelectedItem().toString(), gpsSimulator);
				mainFrame.setObjectDisplayer(new ObjectDisplayer(mainFrame));
				mainFrame.setStarting(true);
				mainFrame.getDisplayThread().start();
				btnStart.setIcon(getImageIcon("/icon/button_cancel.png"));
				btnStart.setText("Stop");
				eastPanel.getPortCombo().setEnabled(false);
				eastPanel.getKaCombo().setEnabled(false);
				eastPanel.getGerbongIsi().setEnabled(false);
				eastPanel.getGerbongKosong().setEnabled(false);
				eastPanel.getRuteCombo().setEnabled(false);
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(mainFrame, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}
		}else{
			CAS cas = mainFrame.getCas();
			cas.stop();
			mainFrame.setStarting(false);
			mainFrame.setDisplayThread(null);
			mainFrame.getListObject().clear();
			btnStart.setIcon(getImageIcon("/icon/button_ok.png"));
			btnStart.setText("Start");
			cas.getMyIdentity().getKeretaApi().setAvailable(true);
			try {
				DAOFactory.createKeretaApiDao().update(cas.getMyIdentity().getKeretaApi());
			} catch (SQLException e) {
				JOptionPane.showMessageDialog(mainFrame, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			}

			eastPanel.getPortCombo().setEnabled(true);
			eastPanel.getKaCombo().setEnabled(true);
			eastPanel.getGerbongIsi().setEnabled(true);
			eastPanel.getGerbongKosong().setEnabled(true);
			eastPanel.getRuteCombo().setEnabled(true);
		}
	}
	private boolean checkParameter(){
		EastPanel eastPanel = mainFrame.getEastPanel();
		if(eastPanel.getPortCombo().getSelectedIndex()==-1){
			JOptionPane.showMessageDialog(mainFrame, "Port harus dipilih", "Warning", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		if(eastPanel.getKaCombo().getSelectedIndex()==-1){
			JOptionPane.showMessageDialog(mainFrame, "Kereta Api harus dipilih", "Warning", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		if(eastPanel.getGerbongIsi().getModel() instanceof SpinnerListModel){
			JOptionPane.showMessageDialog(mainFrame, "Jumlah gerbong isi harus diisi", "Warning", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		if(eastPanel.getGerbongKosong().getModel() instanceof SpinnerListModel){
			JOptionPane.showMessageDialog(mainFrame, "Jumlah gerbong kosong harus diisi", "Warning", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		if(eastPanel.getRuteCombo().getSelectedIndex()==-1){
			JOptionPane.showMessageDialog(mainFrame, "Rute harus dipilih", "Warning", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		return true;
	}
	
	public JLabel getLblSuggest() {
		return lblSuggest;
	}
	public JLabel getLblTimeMsg(){
		return lblTimemsg;
	}
	private ImageIcon getImageIcon(String name){
		return new ImageIcon(SouthPanel.class.getResource(name));
	}
}
